package com.crane.common.core.dto;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.ErrorCode;

import java.text.MessageFormat;
import java.util.Collection;

/**
 * @author zhenxing.pi
 */
@SuppressWarnings("rawtypes")
public class MultiResponse<T> extends Response {

    private static final long serialVersionUID = 9162948712474156556L;

    private ListResponse<T> data;


    public static <T> MultiResponse<T> of(IPage<T> page) {
        MultiResponse<T> multiResponse = new MultiResponse<>();
        multiResponse.setSuccess(true);
        /**
         * 兼容以前格式
         */
        multiResponse.setCode(BasicErrorCode.SERVER_SUCCESS.getErrCode());
        ListResponse<T> list = new ListResponse<>();
        list.setList(page.getRecords());
        list.setTotalCount(page.getTotal());
        list.setPageSize(page.getSize());
        list.setCurrPage(page.getCurrent());
        multiResponse.setData(list);
        return multiResponse;
    }

    public static <T> MultiResponse<T> of(Collection<T> data, int total) {
        MultiResponse<T> multiResponse = new MultiResponse<>();
        multiResponse.setSuccess(true);
        /**
         * 兼容以前格式
         */
        multiResponse.setCode(BasicErrorCode.SERVER_SUCCESS.getErrCode());
        ListResponse<T> list = new ListResponse<>();
        list.setList(data);
        list.setTotalCount(total);
        multiResponse.setData(list);
        return multiResponse;
    }

    public static <T> MultiResponse<T> of(Collection<T> data, int total, PageQuery query) {
        MultiResponse<T> multiResponse = new MultiResponse<>();
        /**
         * 兼容以前格式
         */
        multiResponse.setCode(BasicErrorCode.SERVER_SUCCESS.getErrCode());
        multiResponse.setSuccess(true);
        ListResponse<T> list = new ListResponse<>();
        list.setList(data);
        list.setTotalCount(total);
        list.setCurrPage(query.getPageNo());
        list.setPageSize(query.getPageSize());
        multiResponse.setData(list);
        return multiResponse;
    }

    public static <T> MultiResponse<T> ofWithoutTotal(Collection<T> data) {
        PageQuery query = new PageQuery();
        return of(data, 0, query);
    }

    public static MultiResponse buildFailure(String errMessage) {
        MultiResponse response = new MultiResponse();
        response.setSuccess(false);
        response.setCode(BasicErrorCode.SYS_ERROR.getErrCode());
        response.setErrMsg(errMessage);
        return response;
    }

    public static MultiResponse buildFailure(String errCode, String errMessage) {
        MultiResponse response = new MultiResponse();
        response.setSuccess(false);
        response.setCode(errCode);
        response.setErrMsg(errMessage);
        return response;
    }


    public static MultiResponse buildFailure(ErrorCode errorCode) {
        MultiResponse response = new MultiResponse();
        response.setSuccess(false);
        response.setCode(errorCode.getErrCode());
        response.setErrMsg(errorCode.getErrDesc());
        return response;
    }

    public static MultiResponse buildFailure(ErrorCode errorCode, Object... replacements) {
        MultiResponse response = new MultiResponse();
        response.setSuccess(false);
        response.setCode(errorCode.getErrCode());
        response.setErrMsg(MessageFormat.format(errorCode.getErrDesc(), replacements));
        return response;
    }


    public static MultiResponse buildSuccess() {
        MultiResponse response = new MultiResponse();
        response.setSuccess(true);
        /**
         * 兼容以前格式
         */
        response.setData(new ListResponse());
        response.setCode(BasicErrorCode.SERVER_SUCCESS.getErrCode());
        return response;
    }

    public ListResponse<T> getData() {
        return data;
    }

    public void setData(ListResponse<T> data) {
        this.data = data;
    }
}
